package com.softeem.god.utils;

import com.softeem.god.entity.Blacklist;
import com.softeem.god.service.BlacklistService;
import com.softeem.god.service.UserService;
import com.softeem.god.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 用于生成随机黑名单数据的工具类
 */
@Component
public class GenerateBlacklistData {
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private BlacklistService blacklistService;
    
    /**
     * 生成指定数量的随机黑名单数据
     * @param count 生成数量
     * @return 生成结果信息
     */
    public String generateRandomBlacklists(int count) {
        try {
            // 获取所有用户ID
            List<User> users = userService.list();
            List<Integer> userIds = new ArrayList<>();
            
            for (User user : users) {
                userIds.add(user.getId());
            }
            
            if (userIds.isEmpty()) {
                return "没有找到用户数据，请先添加用户！";
            }
            
            // 生成随机黑名单数据
            List<Blacklist> blacklists = BlacklistDataGenerator.generateRandomBlacklists(userIds, count);
            
            // 保存到数据库
            boolean result = blacklistService.saveBatch(blacklists);
            
            if (result) {
                return "成功生成并保存了" + blacklists.size() + "条黑名单数据！";
            } else {
                return "保存黑名单数据失败！";
            }
        } catch (Exception e) {
            return "生成黑名单数据时发生异常：" + e.getMessage();
        }
    }
}